package com.athuahua.spzx.controller.cargo;

import cn.hutool.core.date.DateTime;
import com.athuahua.spzx.annotation.AutoLog;
import com.athuahua.spzx.entity.Brand;
import com.athuahua.spzx.entity.CategoryBrand;
import com.athuahua.spzx.entity.vo.CategoryBrandVo;
import com.athuahua.spzx.dto.product.CategoryBrandDto;
import com.athuahua.spzx.service.CategoryBrandService;
import com.athuahua.spzx.utils.Result;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: huahua
 * @Date: 2024/7/31 0031
 * @Time: 14:21
 * @Description:
 */
@RestController
@RequestMapping("admin/product/categoryBrand")
@Tag(name = "分类品牌接口")
@Slf4j
public class CategoryBrandController {
    @Resource
    private CategoryBrandService categoryBrandService;

    @GetMapping("pageInfo/{page}/{limit}")
    @Cacheable(cacheNames = "categoryBrandPageInfo",
            key = "{#p0, #p1}",
            condition = "#p2.categoryId == null && #p2.brandId == null")
    public Result<Map<String, Object>> pageInfo(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit, CategoryBrandDto categoryBrandDto) {
        log.info("分类品牌分页查询接口 {} {} {} ", page, limit, categoryBrandDto);
        IPage<CategoryBrandVo> list = categoryBrandService.pageInfo(page, limit, categoryBrandDto);
        Map<String, Object> map = new HashMap<>();
        map.put("list", list.getRecords());
        map.put("total", list.getTotal());

        return Result.success(map);
    }


    //添加 save
    @PostMapping("save")
    @CacheEvict(cacheNames = "categoryBrandPageInfo", allEntries = true)
    @AutoLog(title = "新增分类品牌", businessType = 1) //记录日志使用
    public Result add(@RequestBody CategoryBrand categoryBrand) {
        log.info("save categoryBrand: {}", categoryBrand);
        categoryBrandService.save(categoryBrand);
        return Result.success();
    }


    //修改 update
    @PutMapping("update")
    @CacheEvict(cacheNames = "categoryBrandPageInfo", allEntries = true)
    @AutoLog(title = "修改分类品牌", businessType = 2) //记录日志使用
    public Result update(@RequestBody CategoryBrand categoryBrand) {
        log.info("update categoryBrand: {} {}", categoryBrand, new DateTime());
        categoryBrandService.updateById(categoryBrand);
        return Result.success();
    }

    //删除 delete
    @DeleteMapping("/delete/{id}")
    @CacheEvict(cacheNames = "categoryBrandPageInfo", allEntries = true)
    @AutoLog(title = "删除分类品牌", businessType = 3) //记录日志使用
    public Result delete(@PathVariable("id") Integer id) {
        log.info("delete categoryBrand id: {}", id);
        categoryBrandService.removeById(id);
        return Result.success();
    }

    //根据分类id查询对应的品牌数据
    @GetMapping("/findBrandByCategoryId/{categoryId}")
    public Result findBrandByCategoryId(@PathVariable("categoryId") Integer categoryId) {
        log.info("findBrandByCategoryId categoryId: {}", categoryId);
        List<Brand> list =  categoryBrandService.findBrandByCategoryId(categoryId);


        return Result.success(list);
    }
}
